import axios from "axios";
// import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import baseURL from './baseURL';

const amaxios = axios.create({
  // baseURL: "http://zkxn-testsvr2:39998/",
  // baseURL: "http://192.168.20.109:39998/",
  baseURL: `${baseURL.webServiceUrl}v3/equipment-base`,
  timeout: 15000 // 请求超时时间
});

// request interceptor
amaxios.interceptors.request.use(
  config => {
    config.headers["sysDataBaseName"] = "DB_TDW1_TENANT";
    // config.headers['Content-Type'] = 'multipart/form-data;charset=utf-8'
    config.headers["accessToken"] = getToken("token");
    config.headers["databaseName"] = getToken("databaseName");
    config.headers["organizationList"] = getToken("organizationList");
    config.headers["organizationNo"] = getToken("organizationNo");
    config.headers["virtualSystemNo"] = getToken("virtualSystemNo");
    if (config.url.includes('/queryPageByCondition') || config.url.includes('/queryPageList')) {
      if (config.data !== undefined) {
        if (config.data.entity !== undefined) {
          config.data.entity.orgainzationMark = getToken("organizationNo") || 'YS'
          config.data.entity.organizationNo = getToken("organizationNo") || 'YS'
          if (!config.data?.entity.farming) config.data.entity.farming = getToken("farming") || ' ';
          config.headers["Content-Type"] = "application/json"
        }
      }
    } else {
      if (config.method === 'post') {
        if (config.data.entity !== undefined) {
          config.data.entity.orgainzationMark = getToken("organizationNo") || 'YS'
          config.data.entity.organizationNo = getToken("organizationNo") || 'YS'
          if (!config.data?.entity.farming) config.data.entity.farming = getToken("farming") || ' ';
          config.headers["Content-Type"] = "application/json"
        } else {
          config.data.orgainzationMark = getToken("organizationNo") || 'YS'
          config.data.organizationNo = getToken("organizationNo") || 'YS'
          if (!config.data?.farming) config.data.farming = getToken("farming") || ' ';
        }
      }
    }
    if (config.url.includes('/queryByCondition') || config.url.includes('query')) {
      if (config.data !== undefined) {
        config.data.orgainzationMark = getToken("organizationNo") || 'YS'
        config.data.organizationNo = getToken("organizationNo") || 'YS'
        if (!config.data?.farming) config.data.farming = getToken("farming") || ' ';
        config.headers["Content-Type"] = "application/json"
      }
    }
    if (config.url.includes('/add')) {
      if (config.data !== undefined) {
        if (!config.data?.organizationNo) config.data.organizationNo = getToken("organizationNo") || ''
        if (!config.data?.farmingTypeNo) config.data.farmingTypeNo = getToken("farmingTypeNo") || ''
        if (!config.data?.farming) config.data.farming = getToken("farming") || ' '
        config.data.breedTypeNo = getToken("breedTypeNo") || 'sheep'
        config.data.systemNo = "ZY";
      }
    }
    // config.withCredentials = true
    // if (getToken("accessToken")) {
    //   config.headers["databaseName"] = getToken("databaseName");
    // }
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
amaxios.interceptors.response.use(
  response => {
    const res = response.data;
    return res;
  },
  error => {
    console.log("err" + error);
    // Message({
    //   message: error.message,
    //   type: "error",
    //   duration: 5 * 1000
    // });
    return Promise.reject(error);
  }
);
// 封装get请求
export function get(url, params = {}) {
  var str = "?";
  for (let key in params) {
    var s = "";
    s = key + "=" + params[key];
    if (str === "?") str += s;
    else str += "&" + s;
  }
  var ur1 = url + (str === "?" ? "" : str);
  // 所有接口都添加organizationNo  add-by yl 2023-12-20
  if (getToken("token")) {
    const s = 'orgainzationMark=' + getToken("organizationNo")
    // if (str === "?") str += s;
    if (!url.includes("?") && str === "?") ur1 += s;

    else ur1 += "&" + s;
  }
  return amaxios({
    method: "get",
    url: ur1
  });
}
// 封装post请求
export function post(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: "post",
    data
  };
  // 所有接口都添加organizationNo  add-by yl 2023-12-20
  if (getToken("token")) {
    data['orgainzationMark'] = getToken("organizationNo");
  }
  sendObject.data = data;
  return amaxios(sendObject);
}
// 封装post请求
export function post1(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: "post",
    data
  };
  // 所有接口都添加organizationNo  add-by yl 2023-12-20
  if (getToken("token")) {
    data['orgainzationMark'] = getToken("organizationNo");
  }
  // sendObject.data = JSON.stringify(data);
  return amaxios(sendObject);
}
export default amaxios
